package com.naodong.shenluntiku.mvp.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.naodong.shenluntiku.R;
import com.naodong.shenluntiku.di.component.DaggerAnalysisComponent;
import com.naodong.shenluntiku.di.module.AnalysisModule;
import com.naodong.shenluntiku.mvp.contract.AnalysisContract;
import com.naodong.shenluntiku.mvp.model.bean.Analysis;
import com.naodong.shenluntiku.mvp.model.bean.CollectType;
import com.naodong.shenluntiku.mvp.model.bean.PagingBean;
import com.naodong.shenluntiku.mvp.model.data.api.CommonApi;
import com.naodong.shenluntiku.mvp.presenter.AnalysisPresenter;
import com.naodong.shenluntiku.mvp.view.activity.AnalysisDetailActivityAutoBundle;
import com.naodong.shenluntiku.mvp.view.adapter.AnalysisListAdapter;

import java.util.List;

import butterknife.BindView;
import me.shingohu.man.base.BaseMVPFragment;
import me.shingohu.man.di.component.AppComponent;
import me.shingohu.man.util.UiUtils;

/**
 * Created by hujie on 17/4/21.
 *
 * 视频解析的列表
 */

public class VideoAnalysisListFragment extends BaseMVPFragment<AnalysisPresenter> implements SwipeRefreshLayout.OnRefreshListener, AnalysisContract.View {
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    AnalysisListAdapter mAdapter;
    String moreUrl;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerAnalysisComponent.builder().appComponent(appComponent).analysisModule(new AnalysisModule(this)).build().inject(this);
    }


    @Override
    protected void onViewCreated(Bundle savedInstanceState) {
        swipeRefreshLayout.setOnRefreshListener(this);
    }


    private void initRecycleView() {

        UiUtils.configRecycleView(recyclerView, new LinearLayoutManager(getActivity()));
        mAdapter = new AnalysisListAdapter(R.layout.list_videoanalysis_item);
        mAdapter.bindToRecyclerView(recyclerView);
        mAdapter.disableLoadMoreIfNotFullPage();
        mAdapter.setPreLoadNumber(3);
        mAdapter.setOnLoadMoreListener(() -> {
            if (moreUrl != null) {
                mPresenter.requestSubjectAnalysisList(false, moreUrl);
            } else {
                mAdapter.loadMoreEnd();
            }
        }, recyclerView);

        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Analysis analysis = mAdapter.getItem(position);
            if (analysis != null) {
                startActivity(AnalysisDetailActivityAutoBundle.builder(analysis.getDetailUrl(),analysis.getId(), CollectType.VIDEOANALYSISTYPE_ID).build(getContext()));
            }
        });
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        firstRequest();
    }


    private void firstRequest() {
        String url = CommonApi.APP_URL + "api/videos";
        mPresenter.requestVideoAnalysisList(true, url);
    }


    @Override
    protected int layoutId() {
        return R.layout.f_video_analysis;
    }


    public static VideoAnalysisListFragment getInstance() {
        VideoAnalysisListFragment fragment = new VideoAnalysisListFragment();

        return fragment;
    }


    @Override
    public void onRefresh() {
        firstRequest();
    }

    @Override
    public void showMessage(String msg) {

    }

    @Override
    public void onRequesrStart() {
        swipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void onRequestFinish() {
        swipeRefreshLayout.setRefreshing(false);
        if (mAdapter != null && mAdapter.isLoading()) {
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void setAnalysisListData(boolean isRefresh, PagingBean<List<Analysis>> listPagingBean) {
        if (mAdapter == null) {
            initRecycleView();
        }
        if (isRefresh) {
            mAdapter.setNewData(listPagingBean.getData());
        } else {
            if (listPagingBean.getData() != null && listPagingBean.getData().size() > 0) {
                mAdapter.addData(listPagingBean.getData());

            }
        }
        checkHasMore(listPagingBean);
    }


    private void checkHasMore(PagingBean pagingBean) {
        moreUrl = pagingBean.getNextUrl();
        if (moreUrl != null) {
            mAdapter.setEnableLoadMore(true);
            mAdapter.loadMoreComplete();
        } else {
            mAdapter.loadMoreEnd();
        }
    }
}
